МІНІСТЕРСТВО ОСВІТИ ТА НАУКИ УКРАЇНИ
Національний університет «Львівська політехніка»
Кафедра САПР
Звіт
Про виконання лабораторної роботи №2
З курсу:
«Бази даних»
Тема: ПРЯМИЙ МЕТОД ДОСТУПУ ДО ФАЙЛІВ НА ЗОВНІШНІХ ЗАПАМ’ЯТОВУЮЧИХ ПРИСТРОЯХ.
Мета: Розглянути органiзацiю i ведення файлiв прямого доступу; набути практичнi навички у програмуваннi алгоритмiв доступу хешуванням.
Текст програми:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define recordSize (sizeof (animalRecord) + 2*9)
#define recordCount 10
#define blockSize (recordSize*recordCount)
#define blockCount 10
#define blockOver (blockSize*blockCount+60)
char mainPath[256] = "base.pdb";
char tempPath[] = "srchres.pdb";
char keyTab = 1;
FILE *dbFile = NULL, *tempFile = NULL;
class animalRecord
{
public:
int ID;
int Age;
char Pet[40+2];
char ownerName[40+2];
char Illness[40+2];
int Price;
char Notes[40+2];
float _current;
float _next;
int isSet;
int errorcode;
void Read(FILE *_current = NULL);
void Write(FILE *_current = NULL);
void GoUpToKey(FILE *_current = NULL);
animalRecord();
};
animalRecord::animalRecord()
{
ID = -1;
Age = 0;
_next=_current=-1;
strcpy (Pet,"NULL");
strcpy (ownerName,"NULL");
strcpy (Illness,"NULL");
Price = 0;
strcpy (Notes,"NULL");
errorcode = isSet = 0;
}
void animalRecord::Write(FILE* _current)
{
if (_current == NULL) return;
double cur = 0;
int i = 0;
cur = ftell(_current);
for (i=0;i<recordSize;i++)
fprintf(_current," ");
fseek(_current,cur,SEEK_SET);
fprintf(_current,"%c\n%d\n%d\n%s\n%s\n%s\n%d\n%s\n%06.0f\n%06.0f\n",keyTab,this->ID,this->Age,this->Pet,this->ownerName,this->Illness,this->Price,this->Notes,this->_current,this->_next);
}
void animalRecord::Read(FILE *_current)
{
animalRecord buf=animalRecord();
int eofflag=0, l, read=0;
char cbuf, key=0;
buf.isSet=0;
do
{
if(feof(_current)){eofflag=1;break;}
fscanf(_current,"%c",&key);
}while(key!=keyTab);
if(eofflag)
{
if(!read)
{
buf.errorcode=1;
*this = buf;
return;
}
}
//Считування з файлу--------------------------------------------------------------
fscanf(_current,"\n%d\n",&buf.ID);
fscanf(_current,"%d\n",&buf.Age);
for(l=0, fscanf(_current,"%c",&cbuf);cbuf!='\n';l++)
{
buf.Pet[l]=cbuf;
fscanf(_current,"%c",&cbuf);
}
buf.Pet[l]='\0';
for(l=0, fscanf(_current,"%c",&cbuf);cbuf!='\n';l++)
{
buf.ownerName[l]=cbuf;
fscanf(_current,"%c",&cbuf);
}
buf.ownerName[l]='\0';
for(l=0, fscanf(_current,"%c",&cbuf);cbuf!='\n';l++)
{
buf.Illness[l]=cbuf;
fscanf(_current,"%c",&cbuf);
}
buf.Illness[l]='\0';
fscanf(_current,"%d\n",&buf.Price);
for(l=0, fscanf(_current,"%c",&cbuf);cbuf!='\n';l++)
{
buf.Notes[l]=cbuf;
fscanf(_current,"%c",&cbuf);
}
buf.Notes[l]='\0';
fscanf(_current,"%f\n",&buf._current);
fscanf(_current,"%f\n",&buf._next);
buf.isSet=1;
*this = buf;
}
void animalRecord::GoUpToKey(FILE *_current)
{
int i=0,a;float ad;
char key;
if((ftell(_current))>0)
fseek(_current,-1,SEEK_CUR);
else{return;}
do
{
ad = ftell(_current);
fscanf(_current,"%c",&key);
fseek(_current,ad,SEEK_SET);
if(key=='\n')
a=-2;
else
a=-1;
fseek(_current,a,SEEK_CUR);
}while(key!=keyTab);
fseek(_current,1,SEEK_CUR);}
int reccmp(char str1[47], char str2[47], int count);
float GetBlockAddress (int);
void New();
void Open();
char* SetStr(int);
animalRecord Set (animalRecord set = animalRecord () );
void Insert (animalRecord sav = animalRecord () );
void Delete (animalRecord del = animalRecord () );
void Modify ();
void Find ();
void Print ();
int main()
{textmode(64);
clrscr();
do
{cout<<"Main menu:";
cout<<"\n\t1. Create new base"<<
"\n\t2. Open base"<<
"\n\t3. Insert"<<
"\n\t4. Delete"<<
"\n\t5. Modify"<<
"\n\t6. Find"<<
"\n\t7. Print"<<
"\n\t8. Quit program\n"<<
"Your choise: ";
switch(getch())
{case '1': ...